package com.etc.demo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etc.common.exception.DataOperationException;
import com.etc.common.exception.SysOperationException;
import com.etc.demo.dao.IDemoMapper;
import com.etc.demo.model.DemoModel;
import com.etc.demo.service.IDemoService;
import com.etc.demo.vo.DemoVO;
import com.github.pagehelper.PageHelper;

/**
 * @Title: DemoModel.java
 * @Package com.etc.demo.model
 * @Description: TODO
 * @author 黄宇
 * @date 2016年3月19日 下午6:41:18
 * @version V1.0
 */

@Service("demoService")
public class DemoServiceImpl implements IDemoService {

	@Autowired
	private IDemoMapper demoMapper;

	@Override
	public Integer insert(DemoVO vo) {
		Integer insertId = 0;
		try {
			DemoModel model = vo.VO2Model();
			insertId = this.demoMapper.insert(model);
		} catch (Exception e) {
			String errMsg = String.format("demo保存异常,参数 :vo=%s", vo);
			throw new DataOperationException(errMsg,e);
		}
		return insertId;
	}

	@Override
	public void delete(Integer id) {
		try {
			this.demoMapper.delete(id);
		} catch (Exception e) {
			String errMsg = String.format("demo删除异常,参数:id=%s", id);
			throw new DataOperationException(errMsg,e);
		}
	}

	@Override
	public void update(DemoVO vo) {
		try {
			DemoModel model = vo.VO2Model();
			this.demoMapper.update(model);
		} catch (Exception e) {
			String errMsg = String.format("demo修改异常,参数:vo=%s", vo);
			throw new DataOperationException(errMsg,e);
		}
	}

	@Override
	public DemoVO selectById(Integer id) {
		DemoModel model = this.demoMapper.selectById(id);
		return model.model2VO();
	}

	@Override
	public List<DemoVO> list(Map<String, Object> param) {
		try {
			PageHelper.startPage((Integer) param.get("pageNo"), (Integer) param.get("pageNum"));
			List<DemoModel> modelList = this.demoMapper.list(param);
			List<DemoVO> voList = new ArrayList<DemoVO>();

			DemoVO vo;
			for (DemoModel model : modelList) {
				vo = model.model2VO();
				voList.add(vo);
			}
			return voList;
		} catch (Exception e) {
			String errMsg = String.format("demo查询异常,参数 :param=%s", param);
			throw new SysOperationException(errMsg,e);
		}
	}

	@Override
	public int count(Map<String, Object> param) {
		return this.demoMapper.count(param);
	}

}
